﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using MySql.Data.MySqlClient;

namespace YJ.Data.MySql
{
    public class AppLibrarySubPages : YJ.Data.Interface.IAppLibrarySubPages
    {
        private DBHelper dbHelper = new DBHelper();
        /// <summary>
        /// 构造函数
        /// </summary>
        public AppLibrarySubPages()
        {
        }
        /// <summary>
        /// 添加记录
        /// </summary>
        /// <param name="model">YJ.Data.Model.AppLibrarySubPages实体类</param>
        /// <returns>操作所影响的行数</returns>
        public int Add(YJ.Data.Model.AppLibrarySubPages model)
        {
            string sql = @"INSERT INTO applibrarysubpages
				(ID,AppLibraryID,Name,Address,Ico,Sort,Note) 
				VALUES(@ID,@AppLibraryID,@Name,@Address,@Ico,@Sort,@Note)";
            MySqlParameter[] parameters = new MySqlParameter[]{
				new MySqlParameter("@ID", MySqlDbType.VarChar, 36){ Value = model.ID },
				new MySqlParameter("@AppLibraryID", MySqlDbType.VarChar, 36){ Value = model.AppLibraryID },
				new MySqlParameter("@Name", MySqlDbType.Text, -1){ Value = model.Name },
				new MySqlParameter("@Address", MySqlDbType.Text, -1){ Value = model.Address },
				model.Ico == null ? new MySqlParameter("@Ico", MySqlDbType.Text, -1) { Value = DBNull.Value } : new MySqlParameter("@Ico", MySqlDbType.Text, -1) { Value = model.Ico },
				new MySqlParameter("@Sort", MySqlDbType.Int32, 11){ Value = model.Sort },
				model.Note == null ? new MySqlParameter("@Note", MySqlDbType.Text, -1) { Value = DBNull.Value } : new MySqlParameter("@Note", MySqlDbType.Text, -1) { Value = model.Note }
			};
            return dbHelper.Execute(sql, parameters);
        }
        /// <summary>
        /// 更新记录
        /// </summary>
        /// <param name="model">YJ.Data.Model.AppLibrarySubPages实体类</param>
        public int Update(YJ.Data.Model.AppLibrarySubPages model)
        {
            string sql = @"UPDATE applibrarysubpages SET 
				AppLibraryID=@AppLibraryID,Name=@Name,Address=@Address,Ico=@Ico,Sort=@Sort,Note=@Note
				WHERE ID=@ID";
            MySqlParameter[] parameters = new MySqlParameter[]{
				new MySqlParameter("@AppLibraryID", MySqlDbType.VarChar, 36){ Value = model.AppLibraryID },
				new MySqlParameter("@Name", MySqlDbType.Text, -1){ Value = model.Name },
				new MySqlParameter("@Address", MySqlDbType.Text, -1){ Value = model.Address },
				model.Ico == null ? new MySqlParameter("@Ico", MySqlDbType.Text, -1) { Value = DBNull.Value } : new MySqlParameter("@Ico", MySqlDbType.Text, -1) { Value = model.Ico },
				new MySqlParameter("@Sort", MySqlDbType.Int32, 11){ Value = model.Sort },
				model.Note == null ? new MySqlParameter("@Note", MySqlDbType.Text, -1) { Value = DBNull.Value } : new MySqlParameter("@Note", MySqlDbType.Text, -1) { Value = model.Note },
				new MySqlParameter("@ID", MySqlDbType.VarChar, 36){ Value = model.ID }
			};
            return dbHelper.Execute(sql, parameters);
        }
        /// <summary>
        /// 删除记录
        /// </summary>
        public int Delete(Guid id)
        {
            string sql = "DELETE FROM applibrarysubpages WHERE ID=@ID";
            MySqlParameter[] parameters = new MySqlParameter[]{
				new MySqlParameter("@ID", MySqlDbType.VarChar, 36){ Value = id.ToString() }
			};
            return dbHelper.Execute(sql, parameters);
        }
        /// <summary>
        /// 将DataRedar转换为List
        /// </summary>
        private List<YJ.Data.Model.AppLibrarySubPages> DataReaderToList(MySqlDataReader dataReader)
        {
            List<YJ.Data.Model.AppLibrarySubPages> List = new List<YJ.Data.Model.AppLibrarySubPages>();
            YJ.Data.Model.AppLibrarySubPages model = null;
            while (dataReader.Read())
            {
                model = new YJ.Data.Model.AppLibrarySubPages();
                model.ID = dataReader.GetString(0).ToGuid();
                model.AppLibraryID = dataReader.GetString(1).ToGuid();
                model.Name = dataReader.GetString(2);
                model.Address = dataReader.GetString(3);
                if (!dataReader.IsDBNull(4))
                    model.Ico = dataReader.GetString(4);
                model.Sort = dataReader.GetInt32(5);
                if (!dataReader.IsDBNull(6))
                    model.Note = dataReader.GetString(6);
                List.Add(model);
            }
            return List;
        }
        /// <summary>
        /// 查询所有记录
        /// </summary>
        public List<YJ.Data.Model.AppLibrarySubPages> GetAll()
        {
            string sql = "SELECT * FROM applibrarysubpages";
            MySqlDataReader dataReader = dbHelper.GetDataReader(sql);
            List<YJ.Data.Model.AppLibrarySubPages> List = DataReaderToList(dataReader);
            dataReader.Close();
            return List;
        }
        /// <summary>
        /// 查询记录数
        /// </summary>
        public long GetCount()
        {
            string sql = "SELECT COUNT(*) FROM applibrarysubpages";
            long count;
            return long.TryParse(dbHelper.GetFieldValue(sql), out count) ? count : 0;
        }
        /// <summary>
        /// 根据主键查询一条记录
        /// </summary>
        public YJ.Data.Model.AppLibrarySubPages Get(Guid id)
        {
            string sql = "SELECT * FROM applibrarysubpages WHERE ID=@ID";
            MySqlParameter[] parameters = new MySqlParameter[]{
				new MySqlParameter("@ID", MySqlDbType.VarChar, 36){ Value = id.ToString() }
			};
            MySqlDataReader dataReader = dbHelper.GetDataReader(sql, parameters);
            List<YJ.Data.Model.AppLibrarySubPages> List = DataReaderToList(dataReader);
            dataReader.Close();
            return List.Count > 0 ? List[0] : null;
        }

        /// <summary>
        /// 删除记录
        /// </summary>
        public int DeleteByAppID(Guid id)
        {
            string sql = "DELETE FROM AppLibrarySubPages WHERE AppLibraryID=@ID";
            MySqlParameter[] parameters = new MySqlParameter[]{
				new MySqlParameter("@ID", MySqlDbType.VarChar){ Value = id }
			};
            return dbHelper.Execute(sql, parameters);
        }

        /// <summary>
        /// 查询所有记录
        /// </summary>
        public List<YJ.Data.Model.AppLibrarySubPages> GetAllByAppID(Guid id)
        {
            string sql = "SELECT * FROM AppLibrarySubPages WHERE AppLibraryID=@ID";
            MySqlParameter[] parameters = new MySqlParameter[]{
				new MySqlParameter("@ID", MySqlDbType.VarChar){ Value = id }
			};
            MySqlDataReader dataReader = dbHelper.GetDataReader(sql, parameters);
            List<YJ.Data.Model.AppLibrarySubPages> List = DataReaderToList(dataReader);
            dataReader.Close();
            return List;
        }
    }
}